import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_widget/src/widget/effect/border_widget.dart';

///毛玻璃效果（高斯模糊）
class GlassWidget extends StatelessWidget {
  final Widget? background; //可自定义背景widget，设置了则color无效
  final Widget? child; //不模糊widget,位于模糊层上方
  final Color? color; //底色，默认白
  final double? width;
  final double? height;
  final double? radius; //圆角
  final Color borderColor; //边框颜色
  final double borderWidth; //边框宽度
  final AlignmentGeometry? alignment;
  final double? sigmaX; //参考 [ImageFilter.blur], 默认5
  final double? sigmaY; //参考 [ImageFilter.blur], 默认5

  const GlassWidget({
    Key? key,
    this.color,
    this.background,
    this.child,
    this.width,
    this.height,
    this.radius,
    this.borderColor = Colors.white,
    this.borderWidth = 0,
    this.alignment,
    this.sigmaX,
    this.sigmaY,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var _center = Stack(
      alignment: this.alignment ?? Alignment.centerLeft,
      children: <Widget>[
        Positioned.fill(
          child: (background != null)
              ? background!
              : Container(
                  color: color ?? Colors.transparent,
                ),
        ),
        BackdropFilter(
          filter: ImageFilter.blur(
            sigmaX: sigmaX ?? 5.0,
            sigmaY: sigmaY ?? 5.0,
          ),
          child: child ?? Container(),
        ),
        if (borderWidth > 0)
          Positioned.fill(
            child: BorderWidget(
              borderColor: borderColor,
              borderWidth: borderWidth,
              radius: radius,
            ),
          ),
      ],
    );

    if (radius == null) {
      return width != null || height != null
          ? SizedBox(
              width: width,
              height: height,
              child: _center,
            )
          : _center;
    } else {
      return ClipRRect(
        clipBehavior: Clip.antiAlias,
        borderRadius: BorderRadius.circular(radius!),
        child: width != null || height != null
            ? SizedBox(
                width: width,
                height: height,
                child: _center,
              )
            : _center,
      );
    }
  }
}
